# 修复Node.js路径配置脚本
# 需要以管理员权限运行

Write-Host "Node.js 路径修复脚本" -ForegroundColor Green
Write-Host "================================" -ForegroundColor Green

# 检查管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    Write-Host "错误：请以管理员权限运行此脚本！" -ForegroundColor Red
    exit 1
}

# 查找Node.js安装路径
Write-Host "正在查找Node.js安装路径..." -ForegroundColor Yellow

$nodePaths = @(
    "C:\Program Files\nodejs\node.exe",
    "C:\Program Files (x86)\nodejs\node.exe",
    "$env:APPDATA\npm\node.exe",
    "$env:LOCALAPPDATA\Programs\nodejs\node.exe"
)

$nodeExePath = $null

# 检查PATH环境变量中的node.exe
try {
    $nodeVersion = node --version 2>$null
    if ($nodeVersion) {
        $nodeExePath = (Get-Command node).Source
        Write-Host "在PATH中找到Node.js: $nodeExePath" -ForegroundColor Green
    }
} catch {
    Write-Host "在PATH中未找到Node.js" -ForegroundColor Yellow
}

# 如果PATH中没有找到，检查常见安装路径
if (-not $nodeExePath) {
    foreach ($path in $nodePaths) {
        if (Test-Path $path) {
            $nodeExePath = $path
            Write-Host "找到Node.js安装: $nodeExePath" -ForegroundColor Green
            break
        }
    }
}

# 如果仍然没有找到，尝试搜索整个系统
if (-not $nodeExePath) {
    Write-Host "正在搜索系统中的Node.js..." -ForegroundColor Yellow
    $searchPaths = @("C:\Program Files", "C:\Program Files (x86)", "$env:LOCALAPPDATA\Programs")
    
    foreach ($searchPath in $searchPaths) {
        if (Test-Path $searchPath) {
            $found = Get-ChildItem -Path $searchPath -Recurse -Name "node.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
            if ($found) {
                $nodeExePath = Join-Path $searchPath $found
                Write-Host "搜索到Node.js: $nodeExePath" -ForegroundColor Green
                break
            }
        }
    }
}

if (-not $nodeExePath) {
    Write-Host "错误：无法找到Node.js安装！" -ForegroundColor Red
    Write-Host "请确保已安装Node.js，或者手动指定路径。" -ForegroundColor Yellow
    exit 1
}

Write-Host "`nNode.js路径: $nodeExePath" -ForegroundColor Cyan

# 检查web.config文件
$webConfigPath = "web.config"
if (-not (Test-Path $webConfigPath)) {
    Write-Host "错误：未找到web.config文件！" -ForegroundColor Red
    exit 1
}

# 读取web.config内容
$webConfigContent = Get-Content $webConfigPath -Raw

# 更新Node.js路径
$escapedPath = $nodeExePath -replace '\\', '\\'
$oldPattern = 'nodeProcessCommandLine="[^"]*"'
$newValue = "nodeProcessCommandLine=`"$escapedPath`""

if ($webConfigContent -match $oldPattern) {
    $webConfigContent = $webConfigContent -replace $oldPattern, $newValue
    Write-Host "已更新web.config中的Node.js路径" -ForegroundColor Green
} else {
    Write-Host "警告：未找到nodeProcessCommandLine配置项" -ForegroundColor Yellow
}

# 保存更新后的web.config
try {
    $webConfigContent | Out-File -FilePath $webConfigPath -Encoding UTF8
    Write-Host "web.config已保存" -ForegroundColor Green
} catch {
    Write-Host "保存web.config失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 显示修复后的配置
Write-Host "`n修复后的配置:" -ForegroundColor Cyan
Write-Host "Node.js路径: $nodeExePath"
Write-Host "web.config已更新"

# 验证Node.js版本
try {
    $version = & $nodeExePath --version
    Write-Host "Node.js版本: $version" -ForegroundColor Green
} catch {
    Write-Host "无法获取Node.js版本信息" -ForegroundColor Yellow
}

Write-Host "`n修复完成！请重新启动IIS网站。" -ForegroundColor Green
Write-Host "如果问题仍然存在，请检查：" -ForegroundColor Yellow
Write-Host "1. Node.js是否正确安装" -ForegroundColor Yellow
Write-Host "2. IIS应用程序池配置" -ForegroundColor Yellow
Write-Host "3. 文件权限设置" -ForegroundColor Yellow 